package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyTaiYingHuiRuoData;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  泰银汇诺
 *
 *  @author 吴佳宝
 */
@ApiPlan(productId = "11", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5TaiYingHuiRuo implements ApiOrgPhoneMd5ConvertHandler {

    public static final Long CHANNEL_ID = 1897482244504784898l;

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.GET);

        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("channelId", CHANNEL_ID);
        });
        return map;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getBoolean("data"), false)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyTaiYingHuiRuoData data = new OrgApplyTaiYingHuiRuoData();
        data.setMobile(apiApplyParameter.getPhone());
        data.setChannelId(CHANNEL_ID);
        data.setCity(apiApplyParameter.getCity());
        data.setName(apiApplyParameter.getUserName());
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        //  芝麻分
        data.setZhima(apiFieldConverter.getSesameCreditText());
        //  花呗
        data.setHuabei(apiFieldConverter.getHbLimitText());
        //  白条
        data.setBaitiao(apiFieldConverter.getBtLimitText());
        //  房产
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer house = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .end(1);
            data.setHouse(house);
        }
        //  车产
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            Integer car = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", 0)
                    .end(1);
            data.setCar(car);
        }
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            Integer gjj = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", 0)
                    .end(1);
            data.setHouseFund(gjj);
        }
        //  保单
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            Integer insurance = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                    .end(1);
            data.setPolicy(insurance);
        }

        //  营业执照
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            Integer businesslicense = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                    .end(1);
            data.setBusiness(businesslicense);
        }
        //  贷款额度
        data.setLoanLimit(apiFieldConverter.getLoanAmountInt());
        String json = JsonUtils.toJson(data);
        return json;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
